<?php
/**
 * Created by PhpStorm.
 * User: lyw
 * Date: 2016/5/27
 * Time: 17:36
 */
require_once ADMIN_PATH . '../bs/BsUserManage.php';

class Doctor_auditController extends BaseController {
    public function index(){
        require_once ADMIN_PATH . '../tools/excle/excel.class.php';
        $xls= new excel('UTF-8', false, 'doctors');

        $keshi_list = require ADMIN_PATH . '../config/config.keshi.php';
        $zhicheng_list = require ADMIN_PATH . '../config/config.zhicheng.php';

        $doDoctor = new Do医师详情();
        $doUser = new Do用户();
        $doHospital = new DoHospital();

        $name = $this->request('name');
        $findStatus = $this->request('findStatus');
        $telephone = $this->request("telephone");

        $userType = $this->request('userType', '');
        $title = $this->request('title', '');
        $keshi = $this->request('keshi', '');
        $province = $this->request('province', '');
        $city = $this->request('city', '');
        $isUnsubscribe = $this->request('isUnsubscribe', '');
        $r_time = $this->request('r_time', '');
        $isExport = $this->request('isExport', '');
        $hospitalLevel = $this->request('hospitalLevel', '');

        $where = "b.公众号id=".G_SERV_ID;

        if ($isUnsubscribe == 'yes') {
            $where .= " and b.unsubscribe = 1";
        } elseif ($isUnsubscribe == 'no') {
            $where .= " and b.unsubscribe = 0";
        }

        if ($r_time) {
            $where .= " and b.regist_time > '".$r_time."'";
        }
        if($name){
            $where .= " and a.name like '%".$name."%'";
        }
        if($telephone){
            $where .= " and b.telephone = '".$telephone."'";
        }
        if($findStatus){
            $where .= " and a.状态='".$findStatus."'";
        }
        if ($userType) {
            $where .= " and a.userType = '$userType'";
        }
        if ($title) {
            $where .= " and a.职务 = '$title'";
        }
        if ($keshi) {
            $where .= " and a.专业领域 = '$keshi'";
        }
        if ($province) {
            $where .= " and b.province = '$province'";
        }
        if ($city) {
            $where .= " and b.city = '$city'";
        }
        if ($hospitalLevel) {
            $where .= " and h.level = '$hospitalLevel'";
        }

        $count = $doDoctor->fields("count(a.id)")
            ->leftJoin($doUser,'b','a.用户id = b.id')
            ->leftJoin($doHospital, 'h', 'a.医院 = h.name')
            ->where($where)->getOneField();
        $page = new \tools\Page($count,15);
        $doDoctor = new Do医师详情();
        $doDoctor->fields("a.*, b.name 姓名, b.头像, b.telephone, b.province, b.city, b.unsubscribe, b.name_wx, b.regist_time")
                ->leftJoin($doUser,'b','a.用户id = b.id')
                ->leftJoin($doHospital, 'h', 'a.医院 = h.name')
                ->where($where)
                ->orderby('a.id desc');
        if (!$isExport) {
            $doctorDetails = $doDoctor->offset($page->limit)->limit($page->pagesize)->rows();
            require $this->template;
        } else {
            $doctorDetails = $doDoctor->rows();
            $data = array();
            $data[]=array('姓名','微信名','电话','省','市','医院','职称','科室','状态','注册时间');
            foreach ($doctorDetails as $key => $value) {
                $data[] = array(
                    $value['姓名'],
                    $value['name_wx'],
                    $value['telephone'],
                    $value['province'],
                    $value['city'],
                    $value['医院'],
                    $value['职务'],
                    $value['专业领域'],
                    $value['状态'],
                    $value['regist_time']
                );
            }
            $xls->addArray($data);
            $xls->generateXML('doctors');
        }
    }

    //导出用户信息
    public function export() {
        $status = $this->request('status');
        $tableName = $this->request('table_name');
        $time = $this->request('r_time');
        $bm = new C后台管理员();
        $bm->daochu($tableName,$status,$time);
    }

    //修改医师审核状态
    public function update_doctor_status(){

        $page = $this->request('page');
        $findStatus = $this->request('findStatus');
        $status = $this->request('update_status');
        $doctorId = $this->request('doctor_id','',true);
        $reason = $this->request('reason', '');

        BsUserManage::checkUser($doctorId, $status, $reason);
        $this->redirect('doctor_audit','index',array('page'=>$page,'findStatus'=>$findStatus));
    }


    public function update_user(){
        $doDoctor = new Do医师详情();
        $doUser = new Do用户();

        $zhicheng = require ADMIN_PATH . '../config/config.zhicheng.php';
        $keshi = require ADMIN_PATH . '../config/config.keshi.php';
        $doctorId = $this->request('doctor_id');
        $userId = $this->request("user_id");
//        $doctorData = $doDoctor->fields('a.*, b.头像')
//            ->leftJoin($doUser, 'b', 'a.用户id=b.id')->where('a.id='.$doctorId)->row();
        $doctorData = $doUser->fields('a.name uname, a.telephone, a.头像, b.*')
            ->leftJoin($doDoctor, 'b', 'a.id=b.用户id')->where('a.id='.$userId)
            ->row();
        $doctor = $doctorData->data;
        require $this->template;
    }

    public function update_user_action(){
        $doDoctor = new Do医师详情();
        $doUser = new Do用户();
        $doctorId = $this->request('doctor_id');
        $userId = $this->request('user_id','',true);
        $name = $this->request('name');
        $hospital = $this->request('hospital');
        $description = $this->request('description');
        $certificate_code = $this->request('certificate_code');
        $doctor_post = $this->request('doctor_post');
        $professional_field = $this->request('professional_field');
        $redirect_url = $this->request('redirect_url');
        $note = $this->request('note');
        $telephone = $this->request('telephone');

        $doDoctor->loadById($doctorId);
        $doctor_url = $doDoctor->get_照片();
        $checkTime = $doDoctor->get_checkTime();
        $doUser->loadById($userId);
        $user_url = $doUser->get_头像();
        $pass = $doUser->get_密码();
        if($telephone && !$pass){
            $password = rand(100000,999999);
            $doUser->set_密码($password);
        }
        //修改医师详情
        if(!$doctorId){
            //添加医师
            $doDoctor->set_createTime(date("Y-m-d H:i:s"));
        }
        if(!$checkTime){
            $doDoctor->set_checkTime(date("Y-m-d H:i:s"));
        }
        $doDoctor->set_id($doctorId);
        $doDoctor->set_用户id($userId);
        $doDoctor->set_name($name);
        $doDoctor->set_医院($hospital);
        $doDoctor->set_简介($description);
        $doDoctor->set_专业领域($professional_field);
        $doDoctor->set_职务($doctor_post);
        $doDoctor->set_资格证号($certificate_code);
        $doDoctor->set_note($note);
        $doDoctor->set_状态('通过');
        $path1='../cache/text_img';
        if($_FILES['certificate']['name'] ){

            if(is_dir($path1)){
                mkdir($path1,"0777");
            }
            $certificate_url=Tools::File_Upload($_FILES['certificate'],$path1);
            if($certificate_url == '图片上传失败' || $certificate_url == '图片不存在' || $certificate_url == '文件太大' || $certificate_url == '文件类型不符!'.$_FILES["portrait"]["type"]){
                echo "<script>alert('图片上传失败');</script>";
                return false;
            }
            $doDoctor->set_照片($certificate_url);
        }
        $res1 = $doDoctor->save();
        if($certificate_url && $res1){
            $pathInfo2 = pathinfo($doctor_url);
            $baseName = $pathInfo2['basename'];
            unlink($path1.'/'.$baseName);
        }
        //修改用户信息
        $doUser->set_name($name);
        $doUser->set_id($userId);
        $doUser->set_telephone($telephone);
        $doUser->set_认证(3);
        $path2='../cache/card_img';
        if($_FILES['portrait']['name']){
            if(is_dir($path2)){
                mkdir($path2,"0777");
            }
            $portrait_url=Tools::File_Upload($_FILES['portrait'],$path2);
            if($portrait_url == '图片上传失败' || $portrait_url == '图片不存在' || $portrait_url == '文件太大' || $portrait_url == '文件类型不符!'.$_FILES["portrait"]["type"]){
                echo "<script>alert('图片上传失败');</script>";
                return false;
            }
            $doUser->set_头像($portrait_url);
        }
        $res2 = $doUser->save();
        if($portrait_url && $res2){
            $pathInfo2 = pathinfo($user_url);
            $baseName = $pathInfo2['basename'];
            unlink($path2.'/'.$baseName);
        }
        if($res1 && $res2) {
            if (!$redirect_url) {
                $redirect_url = $this->url('doctor_audit','index',array('page'=>$this->request('page')));
            }
            header("Location:".$redirect_url);
        } else {
            echo "<script>alert('操作失败！');</script>";
        }

    }


}